<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

class Language{
	
    private $lang;

    private static $instance;
    /**
     *
     * @return Language
     */
    public static function get_instance(){
        if (!isset(Language::$instance)){
            Language::$instance = new Language();
        }
        return Language::$instance;
    }

    private function Language() {
        $lang = array();

        $this->generate_lang_object();
    }

    public static function reload_language(){
        Language::$instance = new Language();
    }

    public function get_label($label_code) {
        if(array_key_exists($label_code, $this->lang)){
            return $this->lang[$label_code];
        }
        else {
            return $label_code;
        }
        
    }
	
    private function generate_lang_object() {
        try
        {
            $xml_config = simplexml_load_file('config.xml');

            if (!isset($this->lang))
            {
                //finding language file
                $xml = simplexml_load_file('config.xml');

                switch ($xml->language_config->language){
//                        case 'english' : $this->lang = new English_Language();
//                                break;
//
//                        case 'french' : $this->lang = new French_Language();
//                                break;

                        default : 
                            $this->feed_Lang($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/language/item_en.csv');
                            $this->feed_Lang($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/language/label_en.csv');

                } //switch
            }
        }
        catch (Exception $e) {
                echo 'Exception : ',  $e->getMessage(), "\n";
        }
    }

    private function feed_Lang($name_csv_file){
        $array = array();

        if (($handle = fopen($name_csv_file, 'r')) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
                $this->lang[$data[0]] = $data[1];
            }
            fclose($handle);

        }
    }
	
}

?>